package com.telnetnz.ticketdesk.action.ticket;

import org.apache.log4j.Logger;

import com.rockbb.common.simpleframe.config.SimpleConfig;
import com.telnetnz.ticketdesk.action.base.BasicSessionPageAccAction;
import com.telnetnz.ticketdesk.bean.Ticket;
import com.telnetnz.ticketdesk.bean.TicketComment;
import com.telnetnz.ticketdesk.service.TicketCommentService;
import com.telnetnz.ticketdesk.service.TicketService;

public class TicketCommentEditAction extends BasicSessionPageAccAction
{
	private static Logger logger = Logger.getLogger(TicketCommentEditAction.class);

	public String doAdd()
	{
		CommentAddBean bean = new CommentAddBean();
		if (bean.getComment() != null)
		{
			if (bean.isSubmitted() && bean.validate())
			{
				bean.getComment().setCreatorId(getSession().getSession_uuid());
				bean.getComment().setUpdaterId(getSession().getSession_uuid());
				TicketCommentService.add(bean.getComment());
				TicketCommentService.sendNotification(bean.getComment(), 0);
			}
		}

		return redirect(bean.getRedirect());
	}

	public class CommentAddBean extends RequestBean
	{
		private TicketComment comment;

		public TicketComment getComment() {
			return comment;
		}

		@Override
		protected void loadRequest() 
		{
			comment = new TicketComment();
			comment.initialize();
			comment.setTicketId(getLong("ticket_id", 0L));

			if (isSubmitted())
			{
				comment.setContent(get("comment_content"));
			}
		}

		@Override
		protected boolean validate() 
		{
			boolean flag = true;
			if (comment.getTicketId() == 0L)
			{
				flag = false;
				put_error_msg("ticket_id", langRes.get("global.content-cannot-be-empty"));
			}
			else
			{
				Ticket dummy = TicketService.getTicket(comment.getTicketId());
				if (dummy == null)
				{
					flag = false;
					put_error_msg("ticket_id", langRes.get("project.project-not-exist"));
				}
			}

			if (comment.getContent().length() == 0)
			{
				flag = false;
				put_error_msg("comment_content", langRes.get("global.content-cannot-be-empty"));
			}

			return flag;
		}
	}
}
